package com.shuang.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shuang.common.IDUtils;
import com.shuang.common.JsonUtils;
import com.shuang.common.PageObject;
import com.shuang.mapper.DbOrderMapper;
import com.shuang.mapper.DbOrderRecordMapper;
import com.shuang.pojo.DbOrder;
import com.shuang.pojo.DbOrderExample;
import com.shuang.pojo.DbOrderExample.Criteria;
import com.shuang.pojo.DbOrderRecord;
import com.shuang.pojo.DbReceiveInformation;
import com.shuang.pojo.ShoppingPojo;
import com.shuang.pojo.TempDbOrder;
import com.shuang.pojo._ExtendDbGoods;
import com.shuang.redis.JedisClient;
import com.shuang.service.DbOrderService;
@Service("dbOrderService")
public class DbOrderServiceImpl implements DbOrderService {

	@Resource(name="dbOrderMapper")
	private DbOrderMapper dbOrderMapper;
	@Resource(name="jedisClient")
	private JedisClient jedisClient;
	@Value("${REDIS_ORDER_KEY}")
	private String REDIS_ORDER_KEY;
	
	@Resource(name="dbOrderRecordMapper")
	private DbOrderRecordMapper dbOrderRecordMapper;
	@Override
	public TempDbOrder createTempOrder(Long getuId,DbReceiveInformation dbReceiveInformation, Long o_monmy,List<ShoppingPojo> listSave) {
		TempDbOrder record=new TempDbOrder();
		Long oId=IDUtils.genItemId();
		record.setRiId(dbReceiveInformation.getRiId());
		record.setoId(oId);
		record.setuId(getuId);
		record.setoCreateTime(new Date());
		record.setRiInfo(dbReceiveInformation.getRiAddress()+" "+dbReceiveInformation.getRiName()+" "+dbReceiveInformation.getRiPhone()+" "+dbReceiveInformation.getRiZipCode());
		record.setoStatus((short)0);
		record.setoTotalMonmy(o_monmy);
		 List<DbOrderRecord>  dbOrderRecords=new ArrayList<DbOrderRecord>();
		 DbOrderRecord dbOrderRecord=null;
		for(ShoppingPojo li:listSave){
			dbOrderRecord=new DbOrderRecord();
			dbOrderRecord.setgId(li.getgId());
			dbOrderRecord.setoId(oId);
			dbOrderRecord.setrCeateTime(new Date());
			dbOrderRecord.setrImage(li.getgMainImage());
			dbOrderRecord.setrMoney(li.getgPrice());
			dbOrderRecord.setrNumber(Long.parseLong(String.valueOf(li.getS_number())));
			dbOrderRecord.setrParamData(li.getS_property_data().getBytes());
			dbOrderRecords.add(dbOrderRecord);
		}
		record.setDbOrderRecords(dbOrderRecords);
		jedisClient.hset("REDIS_ORDER_KEY", oId.toString(), JsonUtils.objectToJson(record));
		jedisClient.expire(oId.toString(), 1800);
		return record;
	}
	@Override
	public Map<String, Object> createOrder(Long orderId, String buyMsg) throws Exception {
		Map<String,Object> map=new HashMap<String, Object>();
		String orderStr=jedisClient.hget(REDIS_ORDER_KEY, orderId.toString());
		TempDbOrder tempDbOrder=JsonUtils.jsonToPojo(orderStr, TempDbOrder.class);
		DbOrder dbOrder=new DbOrder();
		dbOrder.setoId(tempDbOrder.getoId());
		dbOrder.setuId(tempDbOrder.getuId());
		dbOrder.setoCreateTime(tempDbOrder.getoCreateTime());
		dbOrder.setRiInfo(tempDbOrder.getRiInfo());
		dbOrder.setoStatus(tempDbOrder.getoStatus());
		dbOrder.setoAudit((short)0);
		dbOrder.setoTotalMonmy(tempDbOrder.getoTotalMonmy());
		dbOrder.setoBuyerMessage(buyMsg);
		int result = dbOrderMapper.insertSelective(dbOrder);
		if(result>0){
			List<DbOrderRecord>  dbOrderRecords=tempDbOrder.getDbOrderRecords();
			for(DbOrderRecord dbOrderRecord:dbOrderRecords){
				int result1 = dbOrderRecordMapper.insert(dbOrderRecord);
				if(result1<=0){
					map.put("status", 500);
					map.put("data", "订单详情生成错误");
					return map;
				}
			}
			map.put("status", 200);
			map.put("data", "订单生成成功");
		}else{
			throw new Exception();
		}
		return map;
	}
	@Override
	public PageObject<DbOrder> getOrderListByStatusAndPage(short status,int pageNum,int pageSize) {
		DbOrderExample example=new DbOrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andOStatusEqualTo(status);
		PageHelper.startPage(pageNum, pageSize);
		List<DbOrder> result = dbOrderMapper.selectByExample(example);
		PageInfo<DbOrder> pageInfo = new PageInfo<DbOrder>(result);
		PageObject<DbOrder> pageObject=new PageObject<DbOrder>();
		pageObject.setTotal(pageInfo.getTotal());
		pageObject.setRows(result);
		return pageObject;
	}
	@Override
	public Map<String, Object> updateOrderStatusByOid(long oId, short status) {
		Map<String,Object> map=new HashMap<String, Object>();
		DbOrder record=new DbOrder();
		record.setoId(oId);
		record.setoStatus(status);
		int result = dbOrderMapper.updateByPrimaryKeySelective(record);
		if(result>0){
			map.put("status", 200);
		}else{
			map.put("status", 500);
			map.put("data", "内部错误，更新失败");
		}
		return map;
	}
	@Override
	public List<DbOrder> selBasicOrderInfo(Long getuId) {
		// TODO 应该要分页查询的
		DbOrderExample example=new DbOrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andUIdEqualTo(getuId);
		List<DbOrder> list = dbOrderMapper.selectByExample(example);
		return list;
	}


}
